﻿using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;

namespace com.fukefu.info.b2b.byhand
{
    public class ESunny : Information
    {
        string siteId = "";
        CookieFactory cf = CookieFactory.GetInstance();

        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("done", "");
            pd.Add("loginVo.loginName", name);
            pd.Add("loginVo.password", pass);

            string html = source.Post("http://member.esunny.com/member/login_post.htm", pd.Read(), "http://member.esunny.com/login.htm");
            long t1 = MyFunction.GetTimestampNow();
            html = source.Get("http://member.esunny.com/member/login_info.htm?t=" + (t1 + random.Next(999)) + "&callback=jsonp" + t1);
            if (html.IndexOf("true") != -1)
            {
                html = source.Get("http://esite.esunny.com/home.htm");
                siteId = Regex.Match(html, "您的商铺地址：<a href=\"http://cn\\.esunny\\.com/([0-9]+)\"").Groups[1].Value;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult info = new InformationResult();
            if (content.Length > 1800)
            {
                content = content.Substring(0, 1700);
            }
            ParametersData pd = new ParametersData();
            pd.Add("companyTrendVo.id", "");
            pd.Add("companyTrendVo.title", title);
            pd.Add("companyTrendVo.content", content);
            pd.Add("checkCode", vcode);

            string html = source.Post("http://esite.esunny.com/companytrend/insert_companytrend.htm", pd.Read(), "http://esite.esunny.com/companytrend/company_trend.htm");
            if (Regex.IsMatch(html, "value=\"([0-9]+)\" class=\"R\"/></td>\\s+<td  align=\"left\"><span class=\"blue2\">" + title))
            {
                info.Code = 1;
                info.Content = "http://cn.esunny.com/" + siteId + "/news/" + Regex.Match(html, "value=\"([0-9]+)\" class=\"R\"/></td>\\s+<td  align=\"left\"><span class=\"blue2\">" + title).Groups[1].Value + ".html";
            }
            else if (Regex.IsMatch(html, "<p class=\"error-text\">\\s*[^<]+?\\s*<p>"))
            {
                info.Content = Regex.Match(html, "<p class=\"error-text\">\\s*([^<]+?)\\s*<p>").Groups[1].Value;
            }
            return info;
        }

        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://member.esunny.com/checkCode.htm?clientSessionId=" + cf.GetCookie("esunny.com", "esessionId") + "&t=" + MyFunction.GetTimestampNow(), "http://esite.esunny.com/companytrend/company_trend.htm");
        }
    }
}